package com.controller;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JOptionPane;

import com.model.Tarefa;
import com.view.TelaMain;

public class SmartAgenda implements ActionListener, Observer{

	private static SmartAgenda smartAgenda;
	
	public TelaMain view;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		smartAgenda = new SmartAgenda();
	}

	public SmartAgenda() {
		EventQueue.invokeLater(new Runnable() {			
			@Override
			public void run() {
				loadAll();
				view = new TelaMain();
				view.setVisible(true);
				view.setActionListener(smartAgenda);
			}
		});
	}

	protected void loadAll() {
		String dir = System.getProperty("user.dir");
		System.out.println(dir);
		DBManager.load();
		for(Tarefa t : DBManager.getAll())
			System.out.println("item: " + t.getDescricao());
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if(source == view.mArCarregar){
			DBManager.load();
			view.updateTarefasCad();
			view.updateCalendar();
			JOptionPane.showMessageDialog(null, "Banco Carregado do Arquivo.");
		}
		if(source == view.mArSalvar){
			DBManager.save();
			JOptionPane.showMessageDialog(null, "Banco Salvo no Arquivo.");
		}
		if(source == view.mArSair){
			view.dispose();
			System.exit(0);
		}
		if(source == view.mEdCadastrar){
			new CadController(this);
			view.setEnabled(false);
		}
		if(source == view.mEdGraficos){
			new GrafController(this);
			//algo da errado nesse
			//view.setEnabled(false);
		}
		if(source == view.mEdGraficosPizza){
			new GrafPizzaController(this);
			//algo da errado nesse
			//view.setEnabled(false);
		}
		if(source == view.mAlManual)
			DBManager.setAllocModSel(DBManager.AllocMod.MANUAL);
		if(source == view.mAlFifo)
			DBManager.setAllocModSel(DBManager.AllocMod.FIFO);
		if(source == view.mAlPrioridade)
			DBManager.setAllocModSel(DBManager.AllocMod.PRIORIDADE);
		if(source == view.mAlDeadline)
			DBManager.setAllocModSel(DBManager.AllocMod.DEADLINE);
	}

	@Override
	public void update(Observable o, Object arg) {
		view.setEnabled(true);
		view.updateTarefasCad();
		view.updateCalendar();
	}

}
